package com.weizhizs.assistant.code.template.data;

import com.weizhizs.assistant.code.config.ParamConfig;
import com.weizhizs.assistant.code.helper.BeanHelper;
import com.weizhizs.assistant.code.helper.TableHelper;
import com.weizhizs.assistant.code.template.TestDataLoador;
import com.weizhizs.assistant.code.vo.TableVO;
import com.weizhizs.assistant.utils.DateUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TestServiceData implements TestDataLoador {

    public static String getClassName(String tableName) {
        return TableHelper.tableNameToClassName(tableName) + "ServiceTest";
    }

    public static String getFullClassName(String classPath, String tableName) {
        return classPath + "." + getClassName(tableName);
    }

    @Override
    public String getTemplateName(TableVO tableVO) {
        return ParamConfig.getInstance().getTemplatePath() + "/test.service.java.vm";
    }

    @Override
    public String getFileName(String tableName) {
        return getClassName(tableName) + ".java";
    }

    @Override
    public String getFileOutPath() {
        return ParamConfig.getInstance().getTestServiceOutPath();
    }

    @Override
    public Map<String, Object> getDataMap(TableVO tableVO) {
        String tableName = tableVO.getTableName();

        Map<String, Object> map = new HashMap<>(32);
        map.put("tableName", tableName);
        map.put("tableComment", tableVO.readTableComment());
        map.put("tableCommentFull", tableVO.getTableComment());
        map.put("authorName", ParamConfig.getInstance().getAuthorName());
        map.put("version", ParamConfig.getInstance().getVersion());
        map.put("currentDate", DateUtils.nowDateString());

        map.put("appServiceOutPath", ParamConfig.getInstance().getAppServiceOutPath());
        map.put("dtoOutPath", ParamConfig.getInstance().getDtoOutPath());
        map.put("queryOutPath", ParamConfig.getInstance().getQueryOutPath());
        map.put("testServiceClassName", getClassName(tableName));
        map.put("appServiceClassName", JavaAppServiceData.getClassName(tableName));

        String baseFullClassName = ParamConfig.getInstance().getServiceBaseClass();
        String baseClassName = BeanHelper.getBeanClassName(ParamConfig.getInstance().getServiceBaseClass());
        map.put("baseFullClassName", baseFullClassName);
        map.put("baseClassName", baseClassName);
        if (TableVO.TABLE_TYPE_DEL == tableVO.getTableType()) {
            baseFullClassName = baseFullClassName.replace(baseClassName, "BaseAllService");
            map.put("baseFullClassName", baseFullClassName);
            map.put("baseClassName", "BaseAllService");
        } else {
            baseFullClassName = baseFullClassName.replace(baseClassName, "BaseEditService");
            map.put("baseFullClassName", baseFullClassName);
            map.put("baseClassName", "BaseEditService");
        }

        map.put("dtoClassName", JavaDtoData.getClassName(tableName));
        map.put("dtoFullClassName", JavaDtoData.getFullClassName(ParamConfig.getInstance().getDtoOutPath(), tableName));

        map.put("queryClassName", JavaQueryData.getClassName(tableName));
        map.put("queryFullClassName", JavaQueryData.getFullClassName(ParamConfig.getInstance().getQueryOutPath(), tableName));

        map.put("pk", tableVO.getPk());
        map.put("allColumns", tableVO.getAllColumns());

        map.put("entityLocalFields", Arrays.asList(ParamConfig.getInstance().getEntityLocalFields()));
        return map;
    }
}
